package com.app.micaihu.view.main.game.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.LazyBaseListFragment;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.game.GameCenterEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.main.game.GameDetailActivity;
import com.app.micaihu.view.main.game.adapter.GameRankApdater;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2017/3/23.
 * <p>
 * <p>
 * 游戏排行的fragment
 */

public class GameRankingFragment extends LazyBaseListFragment<GameCenterEntity> {


    private String type;
    //  1流行榜 2新品榜 3月度榜 4单机榜 5网游榜  6混合游戏榜
    public static final String POPRANK = "1";
    public static final String NEWRANK = "2";
    public static final String MONTHRANK = "3";
    public static final String OFFLINERANK = "4";
    public static final String ONLINERANK = "5";
    public static final String ALLRANK = "6";


    private HashMap<String, String> listMap = new HashMap<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(PARAMETER1);

        }
//        toUid =  getArguments().getString(PARAMETER2);
    }



    @Override
    protected void changeData(final boolean isRefresh) {
//        LogUtils.e("gzq", "chageData = "+isRefresh);

        Map<String, String> params = new HashMap<>();


        params.put("type", type);
        params.put("page", dataPage + "");

        HttpListener<DataBean<List<GameCenterEntity>>> listListener = new HttpListener<DataBean<List<GameCenterEntity>>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<List<GameCenterEntity>> response) {
                if (response.noError() && response.getData() != null) {
                    List<GameCenterEntity> gameList = response.getData();
                    if (gameList.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<GameCenterEntity>();
                        }
                        if (isRefresh) {
                            //清空数据
                            dataList.clear();
                            listMap.clear();
                        }
                        dataRevisited(gameList);
                        dataList.addAll(gameList);
                        if (mAdapter == null) {
                            mAdapter = new GameRankApdater(dataList, getActivity());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);
                    } else {
                        if (isRefresh && dataList != null) {
                            dataList.clear();
                            listMap.clear();
                        }
                        if (dataList == null || dataList.size() == 0) {
                            setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.game_no_game));
                        } else {
                            setLoadView(LOADSUCES, getString(R.string.homepage_msg_nomoremsg));
                        }
                    }

                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };

        addPostCacheRequest(URIContant.GAME_RANKING, new TypeToken<DataBean<List<GameCenterEntity>>>() {
        }.getType(), params, listListener);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        MobclickAgentUtils.event(UmEventId.game_click, UmEventId.INSTANCE.getGamecenter());
        StatService.onEvent(getContext(), "067", "排行榜", 1);//统计事件的点击次数
        GameCenterEntity gameCenterEntity = dataList.get(position);
        if (gameCenterEntity == null) return;
        Intent intent = new Intent();
        intent.putExtra(Config.Extra.PARAMETER1, gameCenterEntity.getArticleId());
        intent.setClass(activity, GameDetailActivity.class);
        startActivity(intent);
    }


    /**
     * 数据去重
     *
     * @param list
     */
    private void dataRevisited(List<GameCenterEntity> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size()) {
                break;
            }
            GameCenterEntity gameCenterEntity = list.get(i);
            if (gameCenterEntity == null) {
                continue;
            }
            //开始去重计算
            if (listMap.get(gameCenterEntity.getPackageName()) == null) {
                listMap.put(gameCenterEntity.getPackageName(), gameCenterEntity.getPackageName());
            } else {
                list.remove(i);//如果包含则替换
                i--;

            }
        }
    }

    public void notifyDown() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }
}

